package com.financialDog.javaTool.services.reportTools;
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSONObject;
import com.financialDog.javaTool.controller.reportTools.ReportMergeToolController;
import com.financialDog.javaTool.reportModel.KeyValue;
import com.financialDog.javaTool.view.reportTools.ReportMergeToolView;
import com.financialDog.javaTool.utils.AnalyticOlapUtils;
import com.xwintop.xcore.util.javafx.JavaFxViewUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Getter
@Setter
@Slf4j
public class ReportMergeToolService{
    private ReportMergeToolController reportMergeToolController;
    private AnalyticOlapUtils analyticOlapUtils;
    private ObservableList<Map<String, String>> tableData = FXCollections.observableArrayList();
    public ReportMergeToolService(ReportMergeToolController reportMergeToolController) {
        this.reportMergeToolController = reportMergeToolController;
    }

    public void setContext(AnalyticOlapUtils analyticOlapUtils){
        this.analyticOlapUtils = analyticOlapUtils;
    }

    public void generateBalanceDataTableView(){
        List<String> columns = new ArrayList<String>();
        //创建报表头
        HashSet<Integer> yearCollection = analyticOlapUtils.getYearCollection();
        columns.add("时间");
        for (Integer year: yearCollection) {
            columns.add(year.toString());
        }
        columns.add("指标");
        int columnIndex = 0;
        TableColumn [] tableColumns = new TableColumn[columns.size()];
        for (String columnName : columns){
            tableColumns[columnIndex] = new TableColumn(columnName);
            JavaFxViewUtil.setTableColumnMapValueFactory(tableColumns[columnIndex], columnName);

            if (columnIndex == 0){
                TableColumn tableColumnReportType = new TableColumn("报表类型");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnReportType, "报表类型");
                tableColumns[columnIndex].getColumns().add(tableColumnReportType);
            }

            if (columnIndex != 0 && columnIndex != columns.size() -1){
                //这里都是年份
                TableColumn tableColumnMerge = new TableColumn("合并报表");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnMerge, "合并报表" + columnName);
                TableColumn tableColumnContain = new TableColumn("占比");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnContain, "占比" + columnName);
                tableColumns[columnIndex].getColumns().add(tableColumnMerge);
                tableColumns[columnIndex].getColumns().add(tableColumnContain);
            }

            if(columnIndex == columns.size() - 1 ){
                TableColumn tableColumnDiscount = new TableColumn("差额");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnDiscount, "差额");
                TableColumn tableColumnChain = new TableColumn("环比");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnChain, "环比");
                tableColumns[columnIndex].getColumns().add(tableColumnDiscount);
                tableColumns[columnIndex].getColumns().add(tableColumnChain);
            }
            columnIndex++;
        }
        this.reportMergeToolController.getTableDataViewMain().getColumns().addAll(tableColumns);

        this.reportMergeToolController.getTableDataViewMain().setItems(analyticOlapUtils.generateMergeBalanceReport());
    }

    public void generateProfitDataTableView(){
        List<String> columns = new ArrayList<String>();
        //创建报表头
        HashSet<Integer> yearCollection = analyticOlapUtils.getYearCollection();
        columns.add("时间");
        for (Integer year: yearCollection) {
            columns.add(year.toString());
        }
        columns.add("指标");
        int columnIndex = 0;
        TableColumn [] tableColumns = new TableColumn[columns.size()];
        for (String columnName : columns){
            tableColumns[columnIndex] = new TableColumn(columnName);
            JavaFxViewUtil.setTableColumnMapValueFactory(tableColumns[columnIndex], columnName);

            if (columnIndex == 0){
                TableColumn tableColumnReportType = new TableColumn("报表类型");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnReportType, "报表类型");
                tableColumns[columnIndex].getColumns().add(tableColumnReportType);
            }

            if (columnIndex != 0 && columnIndex != columns.size() -1){
                //这里都是年份
                TableColumn tableColumnMerge = new TableColumn("合并报表");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnMerge, "合并报表" + columnName);
                TableColumn tableColumnContain = new TableColumn("占比");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnContain, "占比" + columnName);
                tableColumns[columnIndex].getColumns().add(tableColumnMerge);
                tableColumns[columnIndex].getColumns().add(tableColumnContain);
            }

            if(columnIndex == columns.size() - 1 ){
                TableColumn tableColumnDiscount = new TableColumn("差额");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnDiscount, "差额");
                TableColumn tableColumnChain = new TableColumn("环比");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnChain, "环比");
                tableColumns[columnIndex].getColumns().add(tableColumnDiscount);
                tableColumns[columnIndex].getColumns().add(tableColumnChain);
            }
            columnIndex++;
        }
        this.reportMergeToolController.getTableProfitView().getColumns().addAll(tableColumns);

        this.reportMergeToolController.getTableProfitView().setItems(analyticOlapUtils.generateMergeProfitReport());
    }

    public void generateMoneyDataTableView(){
        List<String> columns = new ArrayList<String>();
        //创建报表头
        HashSet<Integer> yearCollection = analyticOlapUtils.getYearCollection();
        columns.add("时间");
        for (Integer year: yearCollection) {
            columns.add(year.toString());
        }
        columns.add("指标");
        int columnIndex = 0;
        TableColumn [] tableColumns = new TableColumn[columns.size()];
        for (String columnName : columns){
            tableColumns[columnIndex] = new TableColumn(columnName);
            JavaFxViewUtil.setTableColumnMapValueFactory(tableColumns[columnIndex], columnName);

            if (columnIndex == 0){
                TableColumn tableColumnReportType = new TableColumn("报表类型");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnReportType, "报表类型");
                tableColumns[columnIndex].getColumns().add(tableColumnReportType);
            }

            if (columnIndex != 0 && columnIndex != columns.size() -1){
                //这里都是年份
                TableColumn tableColumnMerge = new TableColumn("合并报表");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnMerge, "合并报表" + columnName);
//                TableColumn tableColumnContain = new TableColumn("占比");
//                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnContain, "占比" + columnName);
                tableColumns[columnIndex].getColumns().add(tableColumnMerge);
//                tableColumns[columnIndex].getColumns().add(tableColumnContain);
            }

            if(columnIndex == columns.size() - 1 ){
                TableColumn tableColumnDiscount = new TableColumn("差额");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnDiscount, "差额");
                TableColumn tableColumnChain = new TableColumn("环比");
                JavaFxViewUtil.setTableColumnMapValueFactory(tableColumnChain, "环比");
                tableColumns[columnIndex].getColumns().add(tableColumnDiscount);
                tableColumns[columnIndex].getColumns().add(tableColumnChain);
            }
            columnIndex++;
        }
        this.reportMergeToolController.getTableMoneyView().getColumns().addAll(tableColumns);

        this.reportMergeToolController.getTableMoneyView().setItems(analyticOlapUtils.generateMergeMoneyReport());
    }


    public void generateAccountTableView(HashMap<KeyValue, String> accountData){
        List<String> columns = new ArrayList<String>();
        //创建报表头
        HashSet<Integer> yearCollection = analyticOlapUtils.getYearCollection();
        columns.add("指标");
        for (Integer year: yearCollection) {
            columns.add(year.toString());
        }
        int columnIndex = 0;
        TableColumn [] tableColumns = new TableColumn[columns.size()];
        for (String columnName : columns){
            tableColumns[columnIndex] = new TableColumn(columnName);
            JavaFxViewUtil.setTableColumnMapValueFactory(tableColumns[columnIndex], columnName);
            columnIndex++;
        }
        this.reportMergeToolController.getTableAccountView().getColumns().addAll(tableColumns);

        this.reportMergeToolController.getTableAccountView().setItems(analyticOlapUtils.generateAccountReport(accountData));
    }

    public JSONObject initAccountObjectCache(){
        String str = ResourceUtil.readUtf8Str("config/calculate_account.json");
        JSONObject accountObject = JSONObject.parseObject(str);
        return accountObject;
    }
}